package com.ias.agdis_r.manager;

import com.ias.agdis_r.bean.auth.AuthEnum;
import com.ias.agdis_r.bean.document.DocumentResult;
import com.ias.agdis_r.bean.fight.FlightQueryValueChanged;
import com.ias.agdis_r.bean.message.MessageBean;
import com.ias.agdis_r.bean.task.NFTask;
import com.ias.agdis_r.bean.task.Task;
import com.ias.agdis_r.bean.task.TaskInfo;
import com.ias.agdis_r.bean.user.User;
import com.ias.agdis_r.util.S;

import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;

public class DataManager {

	private static DataManager instance = null;// 单例的数据库管理器
	private LinkedHashMap<String,Task> taskWorkList = new LinkedHashMap<String,Task>();
	//接收任务，但未接受的任务
	private LinkedHashMap<String,Task> taskReceiveList = new LinkedHashMap<String,Task>();
	
	private LinkedHashMap<String,Task> taskHistroyWorkList = new LinkedHashMap<String,Task>();

	private LinkedHashMap<String, Task> nfTaskWorkList = new LinkedHashMap<String, Task>();

	private LinkedHashMap<String, Task> nfTaskReceiveList = new LinkedHashMap<String, Task>();

	private LinkedHashMap<String, Task> nfTaskHistroyWorkList = new LinkedHashMap<String, Task>();

	private LinkedHashMap<String, MessageBean> isreadMsgList = new LinkedHashMap<String,MessageBean>();   //已读短语
	private LinkedHashMap<String, MessageBean> unreadMsgList = new LinkedHashMap<String,MessageBean>();	  //未读短语
	
	private List<FlightQueryValueChanged> flightChangeMsgList = new ArrayList<FlightQueryValueChanged>();   //航班变更消息列表，用于显示
	
	private LinkedHashMap<String, DocumentResult> documentMap;
	
	/** 未完成任务TNB列表（初始为null，请求都成功后size为0） */
	private LinkedHashMap<String,String> unfinishedTnbMap = null;   //未完成任务TNB列表  为了保持结构使用Map，实际value无意义
	private LinkedHashMap<String,String> newTnbMap = null;		  //新任务TNB列表，为了保持结构使用Map，实际value无意义

	/** 未完成通用任务TNB列表（初始为null，请求都成功后size为0） */
	private LinkedHashMap<String,String> unfinishedEventTnbMap = null;   //未完成通用任务TNB列表  为了保持结构使用Map，实际value无意义

	/** 数据库读取任务变更数据 */
	private List<Map<String, Object>> taskChangeList = new ArrayList<Map<String, Object>>();

	/** 数据库读取航班变更数据 */
	private List<Map<String, Object>> flightChangeList = new ArrayList<Map<String, Object>>();

	/** 数据库读取信息数据 */
	private List<Map<String, Object>> messageList = new ArrayList<Map<String, Object>>();

	private User[] memberList = null;

	/** 消息盒子上的提示数字 */
	private int UnCheckNumber = 0;

	public static ObjectMapper mapper = new ObjectMapper();

	public ObjectMapper getMapper() {
		if (null == mapper) {
			mapper = new ObjectMapper();
			setMapper();
		}
		return mapper;
	}

	private static void setMapper() {
		if (null!=mapper) {
			mapper.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);
			mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));
			mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
		}
	}

	/**
	 * 单例模式
	 * 
	 * @return
	 */
	public static DataManager getInstance() {
		// TODO Auto-generated method stub
		if (instance == null) {
			instance = new DataManager();
			setMapper();
		}
		return instance;
	}

	public User[] getMemberList(){

		return this.memberList;
	}

	public void setMemberList(User[] memberList){
		this.memberList = memberList;
	}

	public int getUnCheckNumber() {
		return UnCheckNumber;
	}

	public void setUnCheckNumber(int unCheckNumber) {
		UnCheckNumber = unCheckNumber;
	}

	public void addUnCheckNumber() {
		UnCheckNumber++;
	}

	public void subtractUnCheckNumber() {
		UnCheckNumber--;
	}
	
	public List<Map<String, Object>> getFlightChangeList() {
		return flightChangeList;
	}

	public void setFlightChangeList(List<Map<String, Object>> flightChangeList) {
		this.flightChangeList = flightChangeList;
	}

	public List<Map<String, Object>> getMessageList() {
		return messageList;
	}

	public void setMessageList(List<Map<String, Object>> messageList) {
		this.messageList = messageList;
	}

	public List<Map<String, Object>> getTaskChangeList() {
		return taskChangeList;
	}

	public void setTaskChangeList(List<Map<String, Object>> taskChangeList) {
		if (this.taskChangeList != null) {
			this.taskChangeList.clear();
		}
		this.taskChangeList = taskChangeList;
	}

	public int getListCheckNumber(List<Map<String, Object>> list) {
		int number = 0;
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).get("ischeck").equals("0")) {
					number++;
				}
			}
		}
		return number;
	}

	public int getMessageReadedNumber(List<Map<String, Object>> list) {
		int number = 0;
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).get("readed").equals("")) {
					number++;
				}
			}
		}
		return number;
	}

	/**
	 * 获取未完成任务的TNB列表
	 */
	public LinkedHashMap<String,String> getUnfinishedTnbMap(){
		
		return unfinishedTnbMap;
	}

	/**
	 * 保存未完成TNB的LIST
	 */
	public void saveUnfinishedTnbMap(LinkedHashMap<String,String> unfinishedTnbMap){
		
		//如果TNBList为空，保存TNB数据
		if(this.unfinishedTnbMap == null)
		{
			this.unfinishedTnbMap = unfinishedTnbMap;
		}
	}

	/**
	 * 将TNB从 TNB列表 中 删除(航班任务、通用任务)
	 */
	public void removeResponseTnb(String tnb){

		if(this.unfinishedTnbMap!=null&&this.unfinishedTnbMap.size()>0){

			this.unfinishedTnbMap.remove(tnb);
		}

		if(this.unfinishedEventTnbMap!=null&&this.unfinishedEventTnbMap.size()>0){

			this.unfinishedEventTnbMap.remove(tnb);

		}

		if(this.newTnbMap!=null)
		{
			this.newTnbMap.remove(tnb);
		}
	}

	/**
	 * 获取未完成通用任务的TNB列表
	 */
	public LinkedHashMap<String,String> getUnfinishedEventTnbMap(){

		return unfinishedEventTnbMap;
	}

	/**
	 * 保存未完成通用任务TNB的LIST
	 */
	public void saveUnfinishedEventTnbMap(LinkedHashMap<String,String> unfinishedEventTnbMap){

		//如果TNBList为空，保存TNB数据
		if(this.unfinishedEventTnbMap == null)
		{
			this.unfinishedEventTnbMap = unfinishedEventTnbMap;
		}
	}

	/**
	 * 将TNB从 TNB列表 中 删除
	 */
	public void removeResponseEventTnb(String tnb){

		if(this.unfinishedEventTnbMap!=null&&this.unfinishedEventTnbMap.size()>0){

			this.unfinishedEventTnbMap.remove(tnb);
		}

		if(this.newTnbMap!=null)
		{
			this.newTnbMap.remove(tnb);
		}
	}
	
	/**
	 * 获取新任务的TNB列表
	 */
	public LinkedHashMap<String,String> getNewTnbMap(){
		
		return newTnbMap;
	}
	
	/**
	 * 保存新的TNB的LIST
	 */
	public  void saveNewTnbMap(LinkedHashMap<String,String> newTnbMap){
		
		this.newTnbMap = newTnbMap;	
	}
	/**
	 * 清空未完成任务的TNB列表
	 */
	public void cleanUnfinishedTnbMap(){
		
		if(unfinishedTnbMap!=null)
		{
			unfinishedTnbMap.clear();
			unfinishedTnbMap = null;
		}
	}

	/**
	 * 清空未完成任务的TNB列表
	 */
	public void cleanUnfinishedEventTnbMap(){

		if(unfinishedEventTnbMap!=null)
		{
			unfinishedEventTnbMap.clear();
			unfinishedEventTnbMap = null;
		}
	}
	
	/**
	 * 清空新任务的TNB列表
	 */
	public void cleanNewTnbMap(){
		
		if(newTnbMap!=null)
		{
			newTnbMap.clear();
			newTnbMap = null;
		}
	}
	
	/**
	 * 获取单据列表
	 * @return
	 */
	public LinkedHashMap<String, DocumentResult> getDocMap() {
		
		if(documentMap == null)
		{
			documentMap = new LinkedHashMap<String, DocumentResult>();
		}
		return documentMap;
	}
	
	/**
	 * 获取单据列表
	 * @return
	 */
	public void delDoc(String fke) {
		
		if(documentMap != null)
		{
			documentMap.remove(fke);
		}
	}
	
	/**
	 * 清空单据MAP
	 */
	public void cleanDocMap() {
		
		if(documentMap == null)
		{
			documentMap = new LinkedHashMap<String, DocumentResult>();
		}
		documentMap.clear();
	}
	 /**
	  * 根据航班号，获取单据信息LIST
	  * @param tag
	  * @return
	  */
	 public DocumentResult getDocList(String fke) {
			
			if(documentMap == null)
			{
				documentMap = new LinkedHashMap<String, DocumentResult>();
			}
			return documentMap.get(fke);
		}
	 
	/**
	 * 添加单据信息
	 * @param tag
	 * @param document
	 */
	public void putDocList(String fke,DocumentResult documentList) 
	{

		if(documentMap == null)
		{
			documentMap = new LinkedHashMap<String, DocumentResult>();
		}
		documentMap.put(fke, documentList);
	}
	
		
	/**
	 * 删除单据信息
	 * @param tag
	 * @param document
	 */
	public void delDocByFke(String fke) {

			if(documentMap == null)
			{
				documentMap = new LinkedHashMap<String, DocumentResult>();
			}
			documentMap.remove(fke);
	}
	/**
	 * 判断本地是否已经存在此信息
	 * @param tgn
	 * @return
	 */
	public boolean hasMsg(String tgn)
	{
		//如果接收任务列表或工作任务列表不为空
		if(S.isEmpty(tgn)||isreadMsgList.get(tgn)!=null||unreadMsgList.get(tgn)!=null)
		{
			return true;
		}			
		return false;
	}
	
	/**
	 * 获取已读短语数量
	 * @return
	 */
	
	public int getIsreadMsgNum()
	{
		return isreadMsgList.size();
	}
	
	/**
	 * 获取未读短语数量
	 * @return
	 */
	public int getUnreadMsgNum()
	{
		return unreadMsgList.size();
	}
	
	/**
	 * 获取航班变更信息List
	 * @return
	 */
	public List<FlightQueryValueChanged> getFlightChangeMsgList()
	{
		return flightChangeMsgList;
	}
	
	/**
	 * 获取已读短语List
	 * @return
	 */
	public LinkedHashMap<String, MessageBean> getIsreadMsgList()
	{
		return isreadMsgList;
	}
	
	/**
	 * 获取未读短语List
	 * @return
	 */
	public LinkedHashMap<String, MessageBean> getUnreadMsgList()
	{
		return unreadMsgList;
	}
	
	/**
	 * 获取未读短语List
	 * @return
	 */
	public MessageBean getUnreadMsg(int position)
	{
		
		MessageBean msgBean = null;
		
		int i = 0;		
		ListIterator<Map.Entry<String, MessageBean>> sss = new ArrayList<Map.Entry<String, MessageBean>>(
				unreadMsgList.entrySet()).listIterator(unreadMsgList.size());
		
		while (sss.hasPrevious()) {
			Map.Entry<String, MessageBean> entry = sss.previous();

			if (i == position) {
				msgBean = entry.getValue();
				break;
			}
			i++;
		}
		
		return msgBean;
	}
	
	/**
	 * 删除临时工作短语或生产通告信息
	 */
	public void clearMessageList() {

		DataManager.getInstance().getIsreadMsgList().clear();
		DataManager.getInstance().getUnreadMsgList().clear();
	}	
	
	
	/**
	 * 获取消息
	 * @param tgn
	 * @return
	 */
	public MessageBean getMsg(String tgn)
	{
		MessageBean msgBean = isreadMsgList.get(tgn);
		if(msgBean == null)
		{
			msgBean = unreadMsgList.get(tgn);
		}
		return msgBean;
	}

	/**
	 * 获取工作任务信息数量
	 * @param tnb
	 * @return
	 */
	public int getWorkTaskNum() {
		
		return taskWorkList.size();
	}
	/**
	 * 获取非航班任务信息数量
	 * @param tnb
	 * @return
	 */
	public int getNFWorkTaskNum() {

		return nfTaskWorkList.size();
	}

	/**
	 * 获取工作任务信息
	 * @param tnb
	 * @return
	 */
	public Task getWorkTask(String tnb) {
		
		return taskWorkList.get(tnb);
	}

	/**
	 * 获取非航班任务信息
	 * @param tnb
	 * @return
	 */
	public Task getWorkNFTask(String tnb) {

		return nfTaskWorkList.get(tnb);
	}


	/**
	 * 获取工作任务信息
	 * @param tnb
	 * @return
	 */
	
	public Task getWorkTask(int position) {
		
		Task task = null;
		
		int i = 0;		
		ListIterator<Map.Entry<String, Task>> sss = new ArrayList<Map.Entry<String, Task>>(
				taskWorkList.entrySet()).listIterator(taskWorkList.size());
		
		while (sss.hasPrevious()) {
			Map.Entry<String, Task> entry = sss.previous();

			if (i == position) {
				task = entry.getValue();
				break;
			}
			i++;
		}
			
		
		return task;
	}



	public Task getWorkNFTask(int position) {

		Task task = null;

		int i = 0;
		ListIterator<Map.Entry<String, Task>> iter = new ArrayList<Map.Entry<String, Task>>(
				nfTaskWorkList.entrySet()).listIterator(nfTaskWorkList.size());

		while (iter.hasPrevious()) {
			Entry<String, Task> entry = iter.previous();

			if (i == position) {
				task = entry.getValue();
				break;
			}

			i++;
		}

		return task;
	}


	public Task getHistoryWorkTask(int position) {
		Task task = null;
		
		int i = 0;		
		ListIterator<Map.Entry<String, Task>> sss = new ArrayList<Map.Entry<String, Task>>(
				taskHistroyWorkList.entrySet()).listIterator(taskHistroyWorkList.size());
		
		while (sss.hasPrevious()) {
			Map.Entry<String, Task> entry = sss.previous();

			if (i == position) {
				task = entry.getValue();
				break;
			}

			i++;
		}
		
		return task;
	}

	public Task getHistoryWorkNFTask(int position) {
		Task task = null;
		
		int i = 0;		
		ListIterator<Map.Entry<String, Task>> iter = new ArrayList<Map.Entry<String, Task>>(
				nfTaskHistroyWorkList.entrySet()).listIterator(nfTaskHistroyWorkList.size());
		
		while (iter.hasPrevious()) {
			Map.Entry<String, Task> entry = iter.previous();

			if (i == position) {
				task = entry.getValue();
				break;
			}

			i++;
		}
		
		return task;
	}


	/**
	 * 获取工作任务信息列表
	 * @param tnb
	 * @return
	 */
	public LinkedHashMap<String,Task> getWorkTaskList() {

		return taskWorkList;
	}

	/**
	 * 获取非航班任务信息列表
	 * @param tnb
	 * @return
	 */
	public LinkedHashMap<String, Task> getWorkNFTaskList() {
		
		return nfTaskWorkList;
	}
	
	/**
	 * 保存非航班任务信息
	 * 
	 * @param info
	 */
	public void saveWorkNFTask(String tnb,Task task) {
		
		nfTaskWorkList.put(tnb, task);
	}
	/**
	 * 保存工作任务信息
	 * 
	 * @param info
	 */
	public void saveWorkTask(String tnb,Task task) {
		
		taskWorkList.put(tnb, task);
	}

	/**
	 * 删除工作任务
	 * 
	 * @param info
	 */
	public void delWorkTask(String tnb)
	{
		//如果有打印单据权限,删除工作任务时，检查列表中是否还有该航班的任务，如果没有了，
		if(JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_DOCPRINT.toString()))
		{
			Task delTask = taskWorkList.get(tnb);
			String delTaskFke = "";
			if(delTask!=null&&delTask.getTaskInfo()!=null)
			{
				delTaskFke = S.valueOf(delTask.getTaskInfo().getFKE());
				boolean isExistFke = false;
				Iterator<String>  iterator= taskWorkList.keySet().iterator();
				while(iterator.hasNext())
				{
					String taskTnb = (String)iterator.next();
					//如果轮询的是当前的TNB，不做处理
					if(tnb.equals(taskTnb))
					{
						continue;
					}
					else
					{
						Task task =  taskWorkList.get(taskTnb);
						if(task != null)
						{
							TaskInfo taskInfo = task.getTaskInfo();
							//判断当前任务列表中是否还是此，FKE
							if(taskInfo!=null&&taskInfo.getFKE().equals(delTaskFke))
							{
								isExistFke = true;
								break;
							}
						}
					}
				}
				//如果当前任务列表中没有FKE了
				if(!isExistFke)
				{
					delDoc(delTaskFke);
				}
			}
		}
		taskWorkList.remove(tnb);
	}
	
	/**
	 * 删除非航班任务
	 * 
	 * @param info
	 */
	public void delNFWorkTask(String tnb)
	{
		nfTaskWorkList.remove(tnb);
	}
	
	
	/**
	 * 删除工作LIST
	 * 
	 * @param info
	 */
	public void cleanWorkList()
	{
		taskWorkList.clear();
	}


	/**
	 * 删除非航班任务列表
	 *
	 * @param info
	 */
	public void cleanNFWorkList()
	{
		nfTaskWorkList.clear();
	}
	
	
	//===============================================================================
	/**
	 * 获取接收任务信息
	 * @param tnb
	 * @return
	 */
	public Task getReceiveTask(String tnb) {
		
		return taskReceiveList.get(tnb);
	}

	/**
	 * 获取接收非航班任务信息
	 * @param tnb
	 * @return
	 */
	public Task getReceiveNFTask(String tnb) {

		return nfTaskReceiveList.get(tnb);
	}


	/**
	 * 获取接收任务信息
	 * @param tnb
	 * @return
	 */
	public boolean hasTask(String tnb) {

		//如果接收任务列表或工作任务列表不为空
		if(S.isEmpty(tnb)||taskReceiveList.get(tnb)!=null
				||taskWorkList.get(tnb)!=null
				|| nfTaskWorkList.get(tnb) != null
				|| nfTaskReceiveList.get(tnb) != null)
		{
			return true;
		}

		return false;
	}


	public boolean hasNFTask(String tnb) {
		
		//如果接收任务列表或工作任务列表不为空
		if(S.isEmpty(tnb)||nfTaskReceiveList.get(tnb)!=null||nfTaskReceiveList.get(tnb)!=null)
		{
			return true;
		}
		
		return false;
	}
	
	/**
	 * 获取接收任务信息数量
	 * @param tnb
	 * @return
	 */
	public int getReceiveTaskNum() {
		
		return taskReceiveList.size();
	}
	
	/**
	 * 获取接收非航班任务信息数量
	 * @param tnb
	 * @return
	 */
	public int getNFReceiveTaskNum() {
		
		return nfTaskReceiveList.size();
	}
	
	/**
	 * 获取接收任务信息列表
	 * @param tnb
	 * @return
	 */
	public LinkedHashMap<String,Task> getReceiveTaskList() {
		
		return taskReceiveList;
	}
	
	/**
	 * 获取非航班接收任务信息列表
	 * @param tnb
	 * @return
	 */
	public LinkedHashMap<String,Task> getReceiveNFTaskList() {
		
		return nfTaskReceiveList;
	}
	
	
	
	/**
	 * 保存接收任务信息
	 * 
	 * @param info
	 */
	public void saveReceiveTask(String tnb,Task task) {
		
		taskReceiveList.put(tnb,task);
	}
	
	/**
	 * 保存接收非航班任务信息
	 * 
	 * @param info
	 */
	public void saveReceiveNFTask(String tnb, Task task) {
		
		nfTaskReceiveList.put(tnb, task);
	}
	
	
	/**
	 * 删除接收任务信息
	 * 
	 * @param info
	 */
	public void delReceiveTask(String tnb)
	{
		taskReceiveList.remove(tnb);
	}

	/**
	 * 删除接收非航班任务信息
	 * 
	 * @param info
	 */
	public void delNFReceiveTask(String tnb)
	{
		nfTaskReceiveList.remove(tnb);
	}
	
	/**
	 * 删除接收任务信息
	 * 
	 * @param info
	 */
	public void cleanReceiveList()
	{
		taskReceiveList.clear();
	}
	
	public void cleanHistroyWorkList()
	{
		taskHistroyWorkList.clear();
	}

	public void cleanNFHistroyWorkList()
	{
		nfTaskHistroyWorkList.clear();
	}


	/**
	 * 删除接收非航班任务信息
	 * 
	 * @param info
	 */
	public void cleanNFReceiveList()
	{
		nfTaskReceiveList.clear();
	}
	
	

	public LinkedHashMap<String, Task> getTaskHistroyWorkList() {
		return taskHistroyWorkList;
	}


	public void saveTaskHistoryWorkList(String key, Task task) {
		taskHistroyWorkList.put(key, task);
	}
	
	public int getHistoryWorkTaskNum() {
		
		return taskHistroyWorkList.size();
	}

	public Task getHistoryWorkTask(String tnb) {

		return taskHistroyWorkList.get(tnb);
	}
	
	
	
	public LinkedHashMap<String, Task> getNFTaskHistroyWorkList() {
		return nfTaskHistroyWorkList;
	}


	public void saveNFTaskHistoryWorkList(String key, Task task) {
		nfTaskHistroyWorkList.put(key, task);
	}
	
	public int getNFHistoryWorkTaskNum() {
		
		return nfTaskHistroyWorkList.size();
	}

	public Task getNFHistoryWorkTask(String tnb) {

		return nfTaskHistroyWorkList.get(tnb);
	}
}
